package com.ubxtech.cloud.config;

import com.ubxtech.cloud.balance.GrayRoundRobinLoadBalancer;
import com.ubxtech.cloud.client.MsgWebsocketServiceIdClient;
import com.ubxtech.cloud.properties.CloudProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * 灰度负载配置类
 *
 * @author madi
 */

@Configuration
@ConditionalOnProperty(value = "ubxtech.cloud.grayFlag", havingValue = "1" ,matchIfMissing = true)
@LoadBalancerClients(defaultConfiguration = GrayLoadbalancerConfig.class)
public class GrayLoadbalancerConfig {

	@Bean
	public ReactorLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment, LoadBalancerClientFactory loadBalancerClientFactory, CloudProperties cloudProperties,MsgWebsocketServiceIdClient msgWebsocketServiceIdClient) {
		String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
		return new GrayRoundRobinLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name, environment,cloudProperties, msgWebsocketServiceIdClient);
	}


}